package com.yorick.common.config.interceptor;

import com.yorick.common.security.AuthorityInterceptor;
import com.yorick.common.security.IPWhiteListInterceptor;
import com.yorick.common.security.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    @Bean
    public IPWhiteListInterceptor ipWhiteListInterceptor() {
        return new IPWhiteListInterceptor();
    }

    @Bean
    public LoginInterceptor loginHandlerInterceptor() {
        return new LoginInterceptor();
    }

    @Bean
    public AuthorityInterceptor authHandlerInterceptor() {
        return new AuthorityInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册Interceptor拦截器
        InterceptorRegistration ipWhiteListRegistration = registry.addInterceptor(ipWhiteListInterceptor());
        InterceptorRegistration loginRegistration = registry.addInterceptor(loginHandlerInterceptor());
        InterceptorRegistration authRegistration = registry.addInterceptor(authHandlerInterceptor());
        //IP白名单拦截器配置
        ipWhiteListRegistration.addPathPatterns("/**");                      //所有路径都被拦截
        //登录拦截器配置
        loginRegistration.addPathPatterns("/**");                      //所有路径都被拦截
        loginRegistration.excludePathPatterns(                         //添加不拦截路径
                "/common/**",            //公共接口
                "/login/**",             //登录
                "/register/**",          //注册
                "/**/*.html",            //html静态资源
                "/**/*.js",              //js静态资源
                "/**/*.css",             //css静态资源
                "/**/*.woff",
                "/**/*.ttf"
        );
        //权限拦截器配置
        authRegistration.addPathPatterns("/**");                      //所有路径都被拦截
        authRegistration.excludePathPatterns(                         //添加不拦截路径
                "/common/**",            //公共接口
                "/login/**",             //登录
                "/resource/**",          //工具接口
                "/register/**",          //注册
                "/**/*.html",            //html静态资源
                "/**/*.js",              //js静态资源
                "/**/*.css",             //css静态资源
                "/**/*.woff",
                "/**/*.ttf"
        );
    }
}